package org.esgi.ppa.HUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.HashMap;

import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.esgi.ppa.controller.UnitTest;

public class ControlButtonForTestWindow implements ActionListener, KeyListener, PropertyChangeListener{
	private UnitTestingWindow utw;
	File file = null;
	public ControlButtonForTestWindow(UnitTestingWindow utw){
		this.utw = utw;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == utw.bAccept){
			@SuppressWarnings("unused")
			int ID = 0, requests= 0 ,connexions = 0;
			String url = "", method = "";
			
			if(utw.tfUrl.getText().isEmpty()){
				utw.displayFen("Le champs URL est vide", utw);
				return;
			}
			if(null==file){
				utw.displayFen("Ajouter le fichier resulat", utw);
				return;
			}
			url = utw.tfUrl.getText();
			HashMap<String, String> headersMap = new HashMap<String, String>();
			for(int i=0;i<utw.addedHeadersPan.getComponentCount();i++){
				JPanel pan = (JPanel)utw.addedHeadersPan.getComponent(i);
				JLabel lName = (JLabel)pan.getComponent(0);
				String name = lName.getText();
				JLabel lValue = (JLabel)pan.getComponent(1);
				String value = lValue.getText();
				headersMap.put(name, value);
			}
			HashMap<String, String> parametersMap = new HashMap<String, String>();
			for(int i=0;i<utw.addedParametersPan.getComponentCount();i++){
				JPanel pan = (JPanel)utw.addedParametersPan.getComponent(i);
				JLabel lName = (JLabel)pan.getComponent(0);
				String name = lName.getText();
				JLabel lValue = (JLabel)pan.getComponent(1);
				String value = lValue.getText();
				parametersMap.put(name, value);
			}
			
			method = utw.cbMethod.getSelectedItem().toString();
			requests = Integer.parseInt((String)utw.tfNbRequests.getText().toString());
			connexions = Integer.parseInt((String)utw.tfNbConnexions.getText().toString());
			
			utw.getMainWindow().manager.addTest(new UnitTest(utw.getMainWindow().manager.getNextId(),url, method, requests, connexions, headersMap, parametersMap,file));
			utw.getMainWindow().manager.fireTableRowsInserted(utw.getMainWindow().manager.getRowCount() -1, utw.getMainWindow().getManager().getRowCount() -1);
			utw.dispose();
		}
		else if(e.getSource() == utw.bAddHeader){
			utw.addToList(utw.tfHeaderName.getText(), utw.tfHeaderValue.getText(), utw.addedHeadersPan);
		}
		else if(e.getSource() == utw.bAddParameter){
			utw.addToList(utw.tfParameterName.getText(), utw.tfParameterValue.getText(), utw.addedParametersPan);
		}
		else if(e.getSource() == utw.bResult){
			JFileChooser fc = new JFileChooser();
			 fc.showSaveDialog(utw);
        	 file = fc.getSelectedFile();
		}
	}
	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		
		
	}
}
